package com.wc.alorithm_blue_bridge._数据结构.单调队列.Q1207;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayDeque;

/**
 * @Author congge
 * @Date 2023/11/4 21:17
 * @description
 */
public class Answer {
    public static void main(String[] args) {
        Input sc = new Input();
        int n = sc.nextInt();
        int k = sc.nextInt();
        ArrayDeque<Integer> maxQueue = new ArrayDeque<>();
        ArrayDeque<Integer> minQueue = new ArrayDeque<>();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = sc.nextInt();
        }
        int ans;
        for (int i = 0; i < k; i++) {
            while (!maxQueue.isEmpty() && array[i] > maxQueue.getLast()) {
                maxQueue.removeLast();
            }
            maxQueue.addLast(array[i]);
            while (!minQueue.isEmpty() && array[i] < minQueue.getLast()) {
                minQueue.removeLast();
            }
            minQueue.addLast(array[i]);
        }
        ans = maxQueue.getFirst() - minQueue.getFirst();
        for (int i = k; i < n; i++) {
            if (!maxQueue.isEmpty()&&maxQueue.getFirst()==array[i-k]){
                maxQueue.removeFirst();
            }
            if (!maxQueue.isEmpty()&&maxQueue.getFirst()==array[i-k]){
                minQueue.removeFirst();
            }
            while (!maxQueue.isEmpty() && array[i] > maxQueue.getLast()) {
                maxQueue.removeLast();
            }
            maxQueue.addLast(array[i]);
            while (!minQueue.isEmpty() && array[i] < minQueue.getLast()) {
                minQueue.removeLast();
            }
            minQueue.addLast(array[i]);
            ans = Math.max(maxQueue.getFirst() - minQueue.getFirst(),ans);
        }
        System.out.println(ans);
    }

    static class Input {
        public StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }
}
